package com.zheng.http;

import com.zheng.util.HttpProtocolUtil;
import com.zheng.util.StaticResourceUtil;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 *  @author: zheng
 *  @Date: 2020-06-23 17:47
 *  @Description:
 */
public class Response {

    private OutputStream outputStream;

    public Response(OutputStream outputStream){
        this.outputStream=outputStream;
    }

    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    public void outputHtml(String path) throws IOException {
        // 获取静态资源文件的绝对路径
        String absolutResourcePath= StaticResourceUtil.getAbsolutePath(path);

        // 输入静态资源文件
        File file = new File(absolutResourcePath);
        if(file.exists() && file.isFile()) {
            // 读取静态资源文件，输出静态资源
           StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else{
            // 输出404
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }

}
